from celery.utils.serialization import jsonify
from django.db import DatabaseError
from django.shortcuts import render, redirect
from django.views import View
from django import http
import re

from ihome.libs.qiniuyun.qiniu_storage import storage
from useres.models import User
from ihome.utils.response_code import RET
from django_redis import get_redis_connection
from django.contrib.auth import authenticate, login, logout
from django.conf import settings
import json
from django.conf import settings



class UserCenterView(View):
    def get(self,request):

        user = request.user
        # 2、对user进行认证
        if not user.is_authenticated:
            return http.JsonResponse({"errno": RET.SESSIONERR, "errmsg": "用户未登录"})

        # 这里需要进行用户的认证

        data = {
            "avatar": user.avatar if user.avatar else "",
            "create_time": "2017-11-07 01:10:21",
            "mobile": user.mobile,
            "name": user.username,
            "user_id": user.id,
        }
        return http.JsonResponse({"errno": RET.OK, "errmsg": "OK", "data": data})



class AvatarView(View):

    def post(self,request):
        image = request.FILES.get("avatar")
        image_url = storage(image.read())

        # 这里需要数据入库
        user = request.user
        if not user.is_authenticated:
            return http.JsonResponse({"errno": RET.SESSIONERR, "errmsg": "用户未登录"})

        user.avatar = image_url
        user.save()

        data = {
            "data": {
                "avatar_url":settings.QING_URL +  image_url
            },
            "errno": "0",
            "errmsg": "头像上传成功"
        }

        return http.JsonResponse(data)
#
#
#
# class NameModifyView(View):
#
#
#
#
# class AuthenticationView(View):
#
#     def post(self,request):







